

import jieba
from typing import Union, List
import uvicorn
from pydantic import BaseModel
from fastapi import FastAPI, requests, Header, Query, Header
from requests import Request
# from tortoise

app = FastAPI()
from pymongo import MongoClient


mgdb = MongoClient()

db = mgdb.db
all_student = db.crud.find({})

dict_ = {}
list_ = []


@app.get('/mongodb/{item}')
def func(item):
    print(item)
    txt = list(jieba.cut(item, cut_all=True))
    data_list = []
    for i in all_student:
        n = i.get('key')
        nn = list(jieba.cut(n, cut_all=True))

        for s in txt:
            if s in nn:
                data_list.append(n)
    return data_list


@app.post('/mongodb/{item}')
def fun(item: str):
    print(item)
    db.crud.insert_one({"key": item})
    return "添加成功"

#
# print(all_student[1])
# txt = all_student[0].get("info")
#     print("原文:", txt)
#     word_list = jieba.lcut(txt)
#     for ina in word_list:
#         dict_[ina] = dict_.get(ina, 0) + 1
#
#     for i, n in dict_.items():
#         list_.append(i)
#         list_.append(n)
#         print("词:", i, "出现的次数:", n)
#     return list_
# db.crud.insert_one({"QWQ": list_})
# os.system(f'sqlacodegen mysql+pymysql://{"root"}:{"321"}@{"ip"}:{"port"}/{"database"}?charset=utf8 > test.py')

# db.test2.insert_one({"name": "没有名字"})

# db.crud.insert_one({"name": "ljj", "info": "Ctrl, 练习时长2min半", "kw": ["C", "r", "t", "l"]})

# txt = list(all_student)

# word_list = jieba.cut(txt, cut_all=False)
# print(list(word_list))


if __name__ == '__main__':
    uvicorn.run(app, host="0.0.0.0", port=9527)



